package routes_api

import (
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/context"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"my_iris/global"
	"my_iris/utils"
)

func todo(ctx iris.Context) {
	r := ctx.GetCurrentRoute()
	newResp(ctx).Msg("todo: " + r.Name()).Ok()
}

// newResp 输出json用
func newResp(ctx iris.Context) *utils.Resp {
	return utils.NewResp(ctx, global.SiteConf.IsProd)
}

func toObjectID(s string) (objId primitive.ObjectID) {
	objId, _ = primitive.ObjectIDFromHex(s)
	return
}

//FormatPhoneNumberPrefix 格式化手机号加前缀 +86
func FormatPhoneNumberPrefix(phoneNumber string, prefix string) (formattedNumber string) {
	if len(phoneNumber) != 11 {
		return
	}
	if prefix == "" {
		prefix = "+86"
	}
	formattedNumber = prefix + phoneNumber
	return
}

//FormatPhoneNumberProtect 格式化手机号：中间4位显示为*
func FormatPhoneNumberProtect(phoneNumber string) (formattedNumber string) {
	if len(phoneNumber) != 11 {
		return
	}
	formattedNumber = phoneNumber[0:3] + "****" + phoneNumber[7:]
	return
}

// 请求是否是json提交
func IsJsonRequest(ctx iris.Context) bool {
	return ctx.GetContentTypeRequested() == context.ContentJSONHeaderValue
}

// 请求是否是 multipart/form-data 提交
func IsFormData(ctx iris.Context) bool {
	return ctx.GetContentTypeRequested() == context.ContentFormMultipartHeaderValue
}

// 请求是否是 application/x-www-form-urlencoded 提交
func IsFormUrlEncoded(ctx iris.Context) bool {
	return ctx.GetContentTypeRequested() == context.ContentFormHeaderValue
}

// 获取请求body数据,form提交或json提交
func ReadRequestBodyData(ctx iris.Context, dataObjectPtr interface{}) (err error) {
	ctp := ctx.GetContentTypeRequested()
	switch ctp {
	case context.ContentJSONHeaderValue: // application/json
		err = ctx.ReadJSON(dataObjectPtr)
	case context.ContentFormHeaderValue: // application/x-www-form-urlencoded
	case context.ContentFormMultipartHeaderValue: // multipart/form-data
		fallthrough
	default:
		err = ctx.ReadForm(dataObjectPtr)
	}
	return
}
